package logic

import (
	"context"
	"gitee.com/Cauchy_AQ/vxopen/pkg/jwt"
	"gitee.com/Cauchy_AQ/vxopen/rpc/transform/transform"

	"gitee.com/Cauchy_AQ/vxopen/api/internal/svc"
	"gitee.com/Cauchy_AQ/vxopen/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserinfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserinfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserinfoLogic {
	return &UserinfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserinfoLogic) Userinfo(req *types.UserInfoReq) (resp *types.UserInfoResp, err error) {
	myClaims := l.ctx.Value("MyClaims").(jwt.MyClaims)

	rpcResp, err := l.svcCtx.Transformer.Userinfo(l.ctx, &transform.UserInfoReq{
		Mobile: myClaims.Mobile,
		Openid: myClaims.OpenId,
	})
	if err != nil {
		return nil, err
	}
	return &types.UserInfoResp{
		Name:   rpcResp.Name,
		Desc:   rpcResp.Desc,
		Vx:     rpcResp.Vx,
		Qq:     rpcResp.Qq,
		Avatar: rpcResp.Avatar,
		Role: &types.RoleInfo{
			Name:        rpcResp.RoleInfo.Name,
			Permissions: rpcResp.RoleInfo.Permission,
		},
	}, nil
}
